<?php

/**
 * @author zhouguoyan
 * @desc 一般企业管理
 **/

class GeneralController extends AppController {
	var $name = 'General';
	
	var $uses = array(
		'Company', 'CompanyAccount', 'SysParams', 'Job' 
	);
	
	var $layout = 'admin';
	
	/**
	 * @author zhouguoyan
	 * @desc 一般企业详细页面
	 **/
	function detail() {
		if (empty($this->rdCompany->id)) {
			$this->redirect('company/index');
			exit();
		}
		$id = $this->rdCompany->id;
		$company_id = $this->rdCompany->company_id;
		$condition = "\"Company\".\"deleted\" is null AND \"Company\".\"id\"=" . "'$company_id' AND \"SysParams\".\"kbn\"='password_question_id'";
		$this->data = $this->Company->find($condition);
		$data = $this->CompanyAccount->find(
			"\"CompanyAccount\".\"deleted\" is null AND \"CompanyAccount\".\"id\"=" . "'$id'");
		$this->set('data', $data);
	
	}
	
	/**
	 * @author zhouguoyan
	 * @desc 一般企业编辑页面
	 **/
	
	function edit() {
		if (empty($this->rdCompany->id)) {
			$this->redirect('company/index');
			exit();
		}
		$this->Session->del('company_info');
		$id = $this->rdCompany->id;
		$company_id = $this->rdCompany->company_id;
		$condition = "\"Company\".\"deleted\" is null AND \"Company\".\"id\"=" . "'$company_id'";
		$this->data = $this->Company->find($condition, null, 
			" Company.modified DESC");
		$this->set('powers', 
			$this->SysParams->generateList(
				$condition = "kbn='password_question_id'"));
		$this->data['CompanyAccount']['login_pwd'] = '';
		$my_arr = explode("-", $this->data['Company']['company_created']);
		$this->data['Company']['company_created_year'] = $my_arr[0];
		$this->data['Company']['company_created_month'] = $my_arr[1];
		$year = $this->getyears();
		$month = $this->getmonth();
		$this->set('year', $year);
		$this->set('month', $month);
		if ($this->data['Company']['incept_email']) {
			$this->data['Company']['incept_email_confirm'] = $this->data['Company']['incept_email'];
		}
		if ($this->data['Company']['email']) {
			$this->data['Company']['email_confirm'] = $this->data['Company']['email'];
		}
		$data = $this->CompanyAccount->find(
			"\"CompanyAccount\".\"deleted\" is null AND \"CompanyAccount\".\"id\"=" . "'$id'");
		if (empty($this->data['CompanyAccount']['login_id'])) {
			$this->data['CompanyAccount']['login_id'] = $data['CompanyAccount']['login_id'];
			$this->data['CompanyAccount']['name'] = $data['CompanyAccount']['name'];
		}
		
		$this->Session->write('login_pwd', $data['CompanyAccount']['login_pwd']);
		if ($this->Session->check('company_info')) {
			$this->data = $this->Session->read('company_info');
			$this->data['Company']['id'] = $this->rdCompany->company_id;
		}
	
	}
	
	function getyears($start = 1900) {
		$years = array();
		$year = date('Y');
		for($i = 0; $start <= $year; $i++) {
			$years[$year] = $year;
			$year--;
		}
		return $years;
	}
	
	function getmonth() {
		$month = array();
		for($i = 1; $i <= 12; $i++) {
			if ($i < 10) {
				$month['0' . $i] = '0' . $i;
			} else {
				$month[$i] = $i;
			}
		
		}
		return $month;
	}
	
	/**
	 * @author zhouguoyan
	 * @desc 一般企业确认页面
	 **/
	
	function confirm() {
		if (empty($this->rdCompany->id)) {
			$this->redirect('company/index');
			exit();
		}
		
		//20101227 keynes jiang 防止company id被hack，使用session的company id，重置company id
		$this->data['Company']['id'] = $this->rdCompany->company_id;
		//20101230 过滤文本中的html标签 added by keynes yangqingsong  --start 
		$this->data['Company']['company_created'] = Sanitize::html(
			$this->params['data']['Company']['company_created_year'], true) . '-' . Sanitize::html(
			$this->params['data']['Company']['company_created_month'], true);
		$this->data['Company']['address'] = Sanitize::html(
			$this->data['Company']['address'], true);
		$this->data['Company']['introduce'] = Sanitize::html(
			$this->data['Company']['introduce'], true);
		$this->data['Company']['tel2'] = Sanitize::html(
			$this->data['Company']['tel2'], true);
		//20101230 过滤文本中的html标签 added by keynes yangqingsong  --end
		

		$this->Session->write('company_info', $this->data);
		
		$message = $this->Company->General_valid($this->Valid, $this->data);
		//-----20081217 update
		//email地址重复验证
		$email = addslashes($this->data['Company']['email']);
		//$company=$this->data['Company']['id'];
		

		$company = $this->rdCompany->company_id;
		
		$condition = "\"Company\".\"deleted\" is null AND " . "\"Company\".\"email\" = '$email' AND " . "\"Company\".\"id\"<>'$company' AND " . "\"Company\".\"company_type_id\" = 2"; //１：人材紹介会社　２：一般会社
		$check_mail_repeat = $this->Company->find($condition);
		if (!empty($check_mail_repeat)) {
			$message .= $this->Valid->tagErrorMsg('exist', 'Company/email');
		}
		
		//LoginID重复验证
		//20101228 转义特殊字符 added by keynes yangqingsong 
		$this->data['CompanyAccount']['login_id'] = Sanitize::sql(
			$this->data['CompanyAccount']['login_id']);
		$condition = "\"CompanyAccount\".\"deleted\" is null AND " . "\"CompanyAccount\".\"login_id\" = '$login_id'";
		
		if (!empty($company)) {
			$condition .= " AND \"CompanyAccount\".\"company_id\" <> " . $company;
		}
		$company_account = $this->CompanyAccount->findCount($condition);
		if ($company_account > 0) {
			$message .= $this->Valid->tagErrorMsg('exist', 
				'CompanyAccount/login_id');
		}
		
		//-----20081217 update
		

		if ($message) {
			//validate failed
			$this->set("message", $message);
			$this->set('powers', 
				$this->SysParams->generateList(
					$condition = "kbn='password_question_id'"));
			$year = $this->getyears();
			$month = $this->getmonth();
			$this->set('year', $year);
			$this->set('month', $month);
			$this->render('edit');
			
			exit();
		} else {
			//validate success
			if (empty($this->data['CompanyAccount']['login_pwd'])) {
				
				$this->data['CompanyAccount']['login_pwd'] = Sanitize::sql(
					$this->Session->read('login_pwd'));
			}
			$pwd_remain_id = (int)($this->data['Company']['pwd_remain_id']);
			$data_id = $this->SysParams->find(
				"\"SysParams\".\"deleted\" is null " . " AND \"SysParams\".\"kbn\"='password_question_id' " . " AND \"SysParams\".\"id\"=" . $pwd_remain_id);
			$this->set('data_id', $data_id);
			
			$this->render('confirm');
			exit();
		}
	}
	
	/**
	 * @author zhouguoyan
	 * @desc 保存数据库,成功页面
	 **/
	
	function finish() {
		
		if (empty($this->rdCompany->id)) {
			$this->redirect('company/index');
			exit();
		}
		$this->data = $this->Session->read('company_info');
		$this->data['CompanyAccount']['id'] = $this->rdCompany->id;
		$this->data['Company']['id'] = $this->rdCompany->company_id;
		if (empty($this->data['CompanyAccount']['login_pwd'])) {
			
			$this->data['CompanyAccount']['login_pwd'] = $this->Session->read(
				'login_pwd');
		} else {
			$this->data['CompanyAccount']['login_pwd'] = md5(
				$this->data['CompanyAccount']['login_pwd']);
		}
		if ($this->Session->check('company_info')) {
			$this->data['CompanyAccount']['email'] = $this->data['Company']['email'];
			$this->CompanyAccount->save($this->data);
			$this->rdCompany->set($this->data['CompanyAccount']);
			$this->data['Company']['update_id'] = 8;
			$this->Company->save($this->data);
			$this->Job->refreshEmail1($this->data['Company']['email'], 
				$this->data['Company']['id']);
			$this->Job->refreshEmail2($this->data['Company']['incept_email'], 
				$this->data['Company']['id']);
		}
		
		$new_data = $this->CompanyAccount->find(
			"\"CompanyAccount\".\"company_id\"=" . $this->rdCompany->company_id);
		$new_data['CompanyAccount']['company_alias'] = $new_data['Company']['company_alias'];
		$new_data['CompanyAccount']['company_name'] = $new_data['Company']['company_name'];
		$new_data['CompanyAccount']['email'] = $new_data['Company']['email'];
		$this->rdCompany->set($new_data['CompanyAccount']);
		$this->Session->del('login_pwd');
		$this->Session->del('company_info');
	
	}

}
?>